Java Syntax

In java programming, the name of the main class should match the name of the filename.

If the filename is Tutorial.java, then the content of the file will look like.

Example

public class Tutorial {
  public static void main(String[] args) {
    System.out.println("My First Tutorial");
  }
}

In the above example, the class Tutorial has a main function and hence its called as main class.

The main class name Tutorial matches the filename Tutorial.java.

Note: Java language is case-sensitive. Tutorial and tutorial both are different.

The main method

Each java program should have the main method. This main method is always required in a java program.

Java compiler will search for the class with main function, and starts executing the statement(s) inside the main function.

public static void main(String[] args) {
    System.out.println("My First Tutorial");
}

The main method is always static. This means that the main function can be called without creating object with the class name Tutorial.

System.out.println

If you need to print any text to the terminal, then you can use the inbuilt function println which is present in System is a built-in java class and its member out.

System.out.println("My First Tutorial");

The println is used to print the text in a new line.

Note: All the statements should terminate with semicolon (;).


Most Read